package com.slt.developkitweb.controler.company;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.slt.developkitweb.annotation.ShowAPI;
import com.slt.developkitweb.util.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @Author shalongteng
 * @Date 2023/8/31 9:39
 * @Description 解密中移金科网关，travelapi的数据。
 **/
@ShowAPI
@Api(tags = "商旅接口")
@RestController
@RequestMapping("/travel")
public class TravelLogController {
    private String logUrl = "http://callcenter.b2bjoy.com:8080/weblog/LogService?logs=shanglv/b2bjoy-pc-service/digital-api/--digital-api--digital-api.out&app=airportal&only5wFlag=y&method=downloadlogs";
//    private String logUrl = "http://callcenter.b2bjoy.com:8080/weblog/LogService?logs=shanglv/b2bjoy-pc-service/digital-api/digital-api--digital-api--all_20231016.0.log&app=airportal&only5wFlag=on&method=downloadlogs";
//    private String logUrl = "http://callcenter.b2bjoy.com:8080/weblog/LogService?logs=shanglv/b2bjoy-pc-service/digital-api/digital-api--digital-api--all_20231018.0.log&app=airportal&only5wFlag=y&method=downloadlogs";



    /**
     * todo 可以使用WebSocket 实时显示日志
     * 默认返回一万行日志
     */
    @GetMapping("getProdDigitalLog")
    @Operation(summary = "数字人生产日志",
            description = "默认返回一万行日志，最多返回5万行日志，可根据参数lines指定返回的行数")
    @Parameter
    public String getProdDigitalLog(@RequestParam(value = "lines",defaultValue = "10000") String lines,
                                    @RequestParam(value = "searchStr",defaultValue = "") String searchStr,
                                    HttpServletRequest request){
        //获取tar包下载链接
        String html = HttpUtil.get(logUrl);
        String[] split = html.split(" href=\"");
        String tarUrl = split[1].split("\">")[0];

        // 下载tar包（带认证）
        byte[] bytes = downloadWithAuth(tarUrl);
        //解压tar包
        byte[] bytes2 = ZipUtil.unGzip(bytes);
        //日志字符串
        String log = new String(bytes2);

        //从五万行日志中，截取后一万行(默认)返回。
        String userAgent = request.getHeader("User-Agent");
        if(userAgent.contains("Chrome") || userAgent.contains("Safari")){
            return LogUtil.getHtmlLog(lines,log,searchStr);
        }else {
            return LogUtil.getStringLog(lines,log,searchStr);
        }
    }

    /**
     * 带Basic认证的下载方法
     */
    private byte[] downloadWithAuth(String url) {
        // 生成Basic认证信息
        String auth = "qidian" + ":" + "qidian@202412";
        String encodedAuth = Base64.encode(auth.getBytes(StandardCharsets.UTF_8));
        String authHeader = "Basic " + encodedAuth;

        // 使用Hutool的HttpRequest添加认证头并下载
        return HttpRequest.get(url)
                .header(Header.AUTHORIZATION, authHeader)
                .execute()
                .bodyBytes();
    }

}
